<import resource="classpath:alfresco/webscripts/org/alfresco/confluence/util/utils.js">

// Connect to remote Alfresco search service

// Prepare a remote connector to Alfreso service
var alfFolderServiceUrl = "/api";

if ( args.id != null ) {

	alfFolderServiceUrl += "/node/"+args.id.replace(":/", "")+"/children";

}

if ( args.path != null ) {

	alfFolderServiceUrl += "/path/workspace/SpacesStore"+args.path+"/children";

}


var s = new Object();

if (args.orderby == undefined) {
   s.orderBy = 0;
}
else {
	s.orderBy = parseInt(args.orderby);
}

if (args.skip == undefined) {
	s.skipCount = 0;
}
else {
	s.skipCount = parseInt(args.skip);
}

if (args.max == undefined) {
	s.maxItems = 0;
}
else {
	s.maxItems = parseInt(args.max);
}



var alfEndpoint = CMISUtils.endpoint.getEndpoint();

var conn = remote.connect(alfEndpoint);

//var ticket = CMISUtils.ticket.getTicket(conn);

try {

	// retrieve folder
	var queryResult = conn.call(stringUtils.urlEncodeComponent(alfFolderServiceUrl));

	if (queryResult.status == 200) {
	   s.nodes = atom.toFeed(queryResult.response);

	   s.totalResults = s.nodes.length;
	   
	} else {

	   s.error = queryResult.response;

	}


} catch (e) {
 
 	s.error = "ERROR: " + e + "<br/><br/>";

    if (e.rhinoException != null) {
      
        var re = e.rhinoException;
           //model.iftlresult += "Stack Trace: <br/><br/>";

         var stackTrace = re.getStackTrace() ;
         var out2 = " ";
         for each (n in stackTrace) {         
           out2 += n + "<br>&nbsp;&nbsp;&nbsp;";
        }
           s.error += out2 + "<br/>";
    }

 }

 model.s = s;
 
 model.endpoint = alfEndpoint;
  
 model.endpointurl = conn.getDescriptor().getEndpointUrl();
